package com.myaihui.repository.impl;

import com.myaihui.foundation.DO.OrderPageDO;
import com.myaihui.foundation.query.OrderQuery;
import com.myaihui.repository.base.BaseNativeSqlRepository;
import com.myaihui.repository.base.OrderLinkRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * *******************************
 * Created by Tim.
 * form com.matt
 * on 2018/7/19
 * *******************************
 */
@Component
public class OrderLinkRepositoryImpl extends BaseNativeSqlRepository implements OrderLinkRepository {
    @Override
    public List<OrderPageDO> findByConditions(OrderQuery orderQuery) {
        String findSql = "select o.id as id, o.orderNo as orderNo, o.carNo as carNo, o.amount as amount, o.created as created, o.status as status, " +
                " u.mobile AS userMobile, u.nickName AS nickName " +
                " from order_entity o left join wechat_user u on o.wechatUserOpenId = u.openId  WHERE true  ";

        if (StringUtils.isNotBlank(orderQuery.getOrderNo())) {
            findSql += String.format(" and o.orderNo like concat('%s', '%s', '%s')", "%", orderQuery.getOrderNo(), "%");
        }
        if (StringUtils.isNotBlank(orderQuery.getCarNo())) {
            findSql += String.format(" and o.carNo like concat('%s', '%s', '%s')", "%", orderQuery.getCarNo(), "%");
        }

        if (Objects.nonNull(orderQuery.getStatus())) {
            findSql += String.format(" and o.status='%s' ", orderQuery.getStatus());
        }

        if (StringUtils.isNotBlank(orderQuery.getMarketId())) {
            findSql += String.format(" and o.marketId='%s' ", orderQuery.getMarketId());
        } else if(StringUtils.isNotBlank(orderQuery.getPropertyMgrId())){
            findSql += String.format(" and o.propertyMgrId='%s' ", orderQuery.getPropertyMgrId());
        }

        if (StringUtils.isNotBlank(orderQuery.getBegin()))
            findSql += String.format(" and o.created >= '%s' ", orderQuery.getBegin());

        if (StringUtils.isNotBlank(orderQuery.getEnd()))
            findSql += String.format(" and o.created <= '%s' ", orderQuery.getEnd());

        findSql += " order by o.created desc " ;
        findSql += String.format(" limit %s, %s ", orderQuery.getiDisplayStart(),  orderQuery.getiDisplayLength());
        List<OrderPageDO> objectArrayList = sqlObjectList(findSql, OrderPageDO.class);
        return objectArrayList;
    }

    @Override
    public Long countByConditions(OrderQuery orderQuery) {
        String findSql = "select count(o.id) " +
                " from order_entity o left join wechat_user u on o.wechatUserOpenId = u.openId  WHERE true  ";

        if (StringUtils.isNotBlank(orderQuery.getOrderNo())) {
            findSql += String.format(" and o.orderNo like concat('%s', '%s', '%s')", "%", orderQuery.getOrderNo(), "%");
        }
        if (StringUtils.isNotBlank(orderQuery.getCarNo())) {
            findSql += String.format(" and o.carNo like concat('%s', '%s', '%s')", "%", orderQuery.getCarNo(), "%");
        }

        if (Objects.nonNull(orderQuery.getStatus())) {
            findSql += String.format(" and o.status='%s' ", orderQuery.getStatus());
        }

        if (StringUtils.isNotBlank(orderQuery.getMarketId())) {
            findSql += String.format(" and o.marketId='%s' ", orderQuery.getMarketId());
        } else if(StringUtils.isNotBlank(orderQuery.getPropertyMgrId())){
            findSql += String.format(" and o.propertyMgrId='%s' ", orderQuery.getPropertyMgrId());
        }

        if (StringUtils.isNotBlank(orderQuery.getBegin()))
            findSql += String.format(" and o.created >= '%s' ", orderQuery.getBegin());

        if (StringUtils.isNotBlank(orderQuery.getEnd()))
            findSql += String.format(" and o.created <= '%s' ", orderQuery.getEnd());


        List objectArrayList = sqlSingleList(findSql);
        return Long.valueOf(objectArrayList.get(0).toString());
    }
}
